package com.zsg.webview.Util;

import android.content.Context;

import com.android.volley.Cache;
import com.android.volley.Network;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.BasicNetwork;
import com.android.volley.toolbox.DiskBasedCache;
import com.android.volley.toolbox.HurlStack;

/**
 * 单例
 * Created by zzc on 2015/10/22.
 */
public class Net {
    private static Net Instance;
    private Context context;
    private RequestQueue requestQueue;

    /**
     * 多个线程访问，要加锁，避免同时访问，造成创建2个实例
     * @param context
     * @return
     */
    public synchronized static Net getInstance(Context context) {
        if(Instance==null)
            Instance=new Net(context);
        return Instance;
    }

    private Net(Context context) {
        this.context=context;
        // 缓存  （这使用的是磁盘缓存）
        Cache cache=new DiskBasedCache(context.getCacheDir(),1024*1024*10);
        // 网络栈 http数据通信的具体实现
        Network network=new BasicNetwork(new HurlStack());

        //创建并启动请求队列
        requestQueue=new RequestQueue(cache,network);
        requestQueue.start();
    }

    public RequestQueue getRequestQueue(){
        return requestQueue;
    }

    public void addRequest(Request request){
        requestQueue.add(request);
    }

}
